<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="6">
            <a-form-item label="考核名称">
              <a-input placeholder="请输入" v-model="queryParam.checkName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="计划年度">
              <a-date-picker
                mode="year"
                placeholder="请选择年份"
                format="YYYY"
                :allowClear="false"
                value-format="yyyy-MM-dd"
                v-model="queryParam.checkYear"
                :open="yearPickShow"
                style="width: 100%"
                @panelChange="handlePanelChange"
                @openChange="handleOpenChange"
              />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-tabs default-active-key="0" @change="handleTabChange">
      <a-tab-pane v-for="item in tabList" :key="item.key" :tab="item.title">
        <component :role="role" :ref="item.refName" v-if="active == item.key" :is="item.comp"></component>
      </a-tab-pane>
    </a-tabs>
    <!-- table区域-end -->

    <create ref="creatModel" @openModelView="handleAddSubmit"></create>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import columns from './indexColumns'
import create from './create'
import { httpAction, getAction, postAction, deleteAction } from '@/api/manage'
import moment from 'moment'

export default {
  name: 'jh',
  mixins: [JeecgListMixin],
  components: {
    create,
  },
  data() {
    return {
      // 表头
      columns: columns(this),
      yearPickShow: false,
      active: '0',
      tabList: [
        {
          key: '0',
          title: '待审批',
          refName: 'dsTable',
          comp: () => import('./components/dsTable.vue'),
        },
        {
          key: '1',
          title: '已审批',
          refName: 'ysTable',
          comp: () => import('./components/ysTable.vue'),
        },
      ],
      dataSource: [
        {
          序号: 1,
          考核名称: 'xxxxxxxxx',
          年份: '2021',
          类型: '年份',
          审批状态: '待审批',
        },
      ],
      queryParam: {
        checkName: '',
        checkYear: '',
        auditFlag:'0'
      },
      role: '3',
      url: {
        list: '/performcheck/checkDeptInfo/queryPendingList',
        role: '/performcheck/checkDeptInfo/getOrgLevel',
      },
    }
  },
  mounted() {
    this.getRole()
  },
  methods: {
    searchQuery () {
      if (this.active == '0') {
        this.$refs.dsTable[0].handleModelSubmit(this.queryParam)
      } else {
        this.$refs.ysTable[0].handleModelSubmit(this.queryParam)
      }
    },
    searchReset() {
      this.queryParam = this.$options.data().queryParam;
      if (this.active == '0') {
        this.$refs.dsTable[0].handleModelSubmit(this.queryParam)
      } else {
        this.$refs.ysTable[0].handleModelSubmit(this.queryParam)
      }
    },
    openCreatModel() {
      this.$refs.creatModel.visible = true
    },
    async getRole() {
      this.role = await getAction(this.url.role)
    },
    handleAddSubmit(evt) {
      return new Promise((re, rj) => {
        postAction(this.url.add, evt).then((res) => {
          if (res.success) {
            this.$refs.table.loadData()
            this.$message.success(res.message)
            re(res.success)
          } else {
            rj(res.success)
            this.$message.error(res.message)
          }
        })
      })
    },
    handlePanelChange(value) {
      this.queryParam.checkYear = moment(value).format('YYYY')
      this.yearPickShow = false
      this.$refs.form.validateField('checkYear', (error) => {
        console.log(error)
      })
    },
    // 弹出日历和关闭日历的回调
    handleOpenChange(status) {
      this.yearPickShow = status
      if(this.queryParam.checkYear){
        this.queryParam.checkYear = moment(this.queryParam.checkYear.slice(0, 4)).format('YYYY')
      }
    },
    handleTabChange(e) {
      this.active = e
      this.queryParam.auditFlag = e
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>